package com.xidian.zhiancloud.service.cgs.TrainManager;

import com.xidian.zhiancloud.dao.TrainContentMapper;
import com.xidian.zhiancloud.dao.TrainContentMapperExt;
import com.xidian.zhiancloud.dao.TrainPlanMapper;
import com.xidian.zhiancloud.dao.TrainPlanMapperExt;
import com.xidian.zhiancloud.dto.TrainDto.TrainPlanAndTrainContentsDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.*;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.xidian.zhiancloud.util.tools.ObjectUtil.isEmpty;

/**
 * @Author: chenguosong
 * @Date: 2020/11/20 00:34
 * @mail 2581592671@qq.com
 */
@Service
public class TrainPlanService {

    @Autowired
    private TrainPlanMapper trainPlanMapper;

    @Autowired
    private TrainPlanMapperExt trainPlanMapperExt;

    @Autowired
    private TrainContentMapper trainContentMapper;

    @Autowired
    private TrainContentMapperExt trainContentMapperExt;

    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public void add(TrainPlanAndTrainContentsDTO trainPlanAndTrainContentsDTO) {
        int result = trainPlanMapperExt.addReturnId(trainPlanAndTrainContentsDTO.getTrainPlan());
        if (result == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        } else if (result > 0) {
            for (TrainContent trainContent : trainPlanAndTrainContentsDTO.getTrainContents()) {
                trainContent.setTrainPlanId(trainPlanAndTrainContentsDTO.getTrainPlan().getId());
                int tmp = trainContentMapper.insert(trainContent);
                if (tmp == 0) {
                    throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
                }
            }
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public void deletebyid(Long id) {
        trainPlanMapper.deleteByPrimaryKey(id);
        TrainContentKey trainContentKey = new TrainContentKey();
        trainContentKey.setTrainPlanId(id);
        trainContentMapperExt.deleteById(trainContentKey);
    }

    public List<TrainPlan> getListPlan(int current, int size) {
        TrainPlanExample trainPlanExample = new TrainPlanExample();
        return trainPlanMapper.selectByExampleWithRowbounds(trainPlanExample, new RowBounds(current, size));
    }

    public List<TrainContent> getListPlanContent(int current, int size, Long trainPlanId) {
        TrainContentExample trainContentExample = new TrainContentExample();
        trainContentExample.createCriteria().andTrainPlanIdEqualTo(trainPlanId);
        return trainContentMapper.selectByExampleWithRowbounds(trainContentExample, new RowBounds(current, size));
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public void update(TrainPlanAndTrainContentsDTO trainPlanAndTrainContentsDTO) {
        if(!isEmpty(trainPlanAndTrainContentsDTO)){
            trainPlanMapper.updateByPrimaryKeySelective(trainPlanAndTrainContentsDTO.getTrainPlan());
            if(!isEmpty(trainPlanAndTrainContentsDTO.getTrainContents())){
                for (TrainContent trainContent : trainPlanAndTrainContentsDTO.getTrainContents()) {
                    trainContent.setTrainPlanId(trainPlanAndTrainContentsDTO.getTrainPlan().getId());
                    int tmp = trainContentMapper.updateByPrimaryKeySelective(trainContent);
                    if (tmp == 0) {
                        throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
                    }
                }
            }
        }else{
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }
}
